using cjx.ProxySocket;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
internal sealed class Class5 : Class3
{
	public Class5(Socket socket_1, string string_1) : base(socket_1, string_1)
	{
	}
	private byte[] method_13(string string_1, int int_0)
	{
		if (string_1 == null)
		{
			throw new ArgumentNullException();
		}
		if (int_0 <= 0 || int_0 > 65535)
		{
			throw new ArgumentException();
		}
		byte[] array = new byte[10 + base.method_5().Length + string_1.Length];
		array[0] = 4;
		array[1] = 1;
		Array.Copy(base.method_0(int_0), 0, array, 2, 2);
		byte[] arg_59_0 = array;
		int arg_59_1 = 4;
		byte[] arg_57_0 = array;
		int arg_57_1 = 5;
		array[6] = 0;
		arg_57_0[arg_57_1] = 0;
		arg_59_0[arg_59_1] = 0;
		array[7] = 1;
		Array.Copy(Encoding.ASCII.GetBytes(base.method_5()), 0, array, 8, base.method_5().Length);
		array[8 + base.method_5().Length] = 0;
		Array.Copy(Encoding.ASCII.GetBytes(string_1), 0, array, 9 + base.method_5().Length, string_1.Length);
		array[9 + base.method_5().Length + string_1.Length] = 0;
		return array;
	}
	private byte[] method_14(IPEndPoint ipendPoint_0)
	{
		if (ipendPoint_0 == null)
		{
			throw new ArgumentNullException();
		}
		byte[] array = new byte[9 + base.method_5().Length];
		array[0] = 4;
		array[1] = 1;
		Array.Copy(base.method_0(ipendPoint_0.Port), 0, array, 2, 2);
		Array.Copy(base.method_1(ipendPoint_0.Address.Address), 0, array, 4, 4);
		Array.Copy(Encoding.ASCII.GetBytes(base.method_5()), 0, array, 8, base.method_5().Length);
		array[8 + base.method_5().Length] = 0;
		return array;
	}
	public override void vmethod_0(string string_1, int int_0)
	{
		this.method_15(this.method_13(string_1, int_0));
	}
	public override void vmethod_1(IPEndPoint ipendPoint_0)
	{
		this.method_15(this.method_14(ipendPoint_0));
	}
	private void method_15(byte[] byte_1)
	{
		if (byte_1 == null)
		{
			throw new ArgumentNullException();
		}
		if (byte_1.Length < 2)
		{
			throw new ArgumentException();
		}
		base.method_3().Send(byte_1);
		byte[] array = base.method_2(8);
		if (array[1] != 90)
		{
			base.method_3().Close();
			throw new ProxyException("Negotiation failed.");
		}
	}
	public override Class6 vmethod_3(string string_1, int int_0, Delegate0 delegate0_1, IPEndPoint ipendPoint_0)
	{
		this.delegate0_0 = delegate0_1;
		base.method_10(this.method_13(string_1, int_0));
		base.method_3().BeginConnect(ipendPoint_0, new AsyncCallback(this.method_16), base.method_3());
		base.method_8(new Class6());
		return base.method_7();
	}
	public override Class6 vmethod_2(IPEndPoint ipendPoint_0, Delegate0 delegate0_1, IPEndPoint ipendPoint_1)
	{
		this.delegate0_0 = delegate0_1;
		base.method_10(this.method_14(ipendPoint_0));
		base.method_3().BeginConnect(ipendPoint_1, new AsyncCallback(this.method_16), base.method_3());
		base.method_8(new Class6());
		return base.method_7();
	}
	private void method_16(IAsyncResult iasyncResult_0)
	{
		try
		{
			base.method_3().EndConnect(iasyncResult_0);
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
			return;
		}
		try
		{
			base.method_3().BeginSend(base.method_9(), 0, base.method_9().Length, SocketFlags.None, new AsyncCallback(this.method_17), base.method_3());
		}
		catch (Exception exception_2)
		{
			this.delegate0_0(exception_2);
		}
	}
	private void method_17(IAsyncResult iasyncResult_0)
	{
		try
		{
			if (base.method_3().EndSend(iasyncResult_0) < base.method_9().Length)
			{
				this.delegate0_0(new SocketException());
				return;
			}
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
			return;
		}
		try
		{
			base.method_10(new byte[8]);
			base.method_12(0);
			base.method_3().BeginReceive(base.method_9(), 0, base.method_9().Length, SocketFlags.None, new AsyncCallback(this.method_18), base.method_3());
		}
		catch (Exception exception_2)
		{
			this.delegate0_0(exception_2);
		}
	}
	private void method_18(IAsyncResult iasyncResult_0)
	{
		try
		{
			int num = base.method_3().EndReceive(iasyncResult_0);
			if (num <= 0)
			{
				this.delegate0_0(new SocketException());
			}
			else
			{
				base.method_12(base.method_11() + num);
				if (base.method_11() == 8)
				{
					if (base.method_9()[1] == 90)
					{
						this.delegate0_0(null);
					}
					else
					{
						base.method_3().Close();
						this.delegate0_0(new ProxyException("Negotiation failed."));
					}
				}
				else
				{
					base.method_3().BeginReceive(base.method_9(), base.method_11(), base.method_9().Length - base.method_11(), SocketFlags.None, new AsyncCallback(this.method_18), base.method_3());
				}
			}
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
		}
	}
}
